package com.share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.constant.MessageConstant;
import com.share.common.core.exception.ServiceException;
import com.share.domain.MySql.Cabinet;
import com.share.domain.MySql.CabinetSlot;
import com.share.domain.MySql.PowerBank;
import com.share.mapper.CabinetMapper;
import com.share.mapper.CabinetSlotMapper;
import com.share.service.ICabinetService;
import com.share.service.PowerBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CabinetServiceImpl extends ServiceImpl<CabinetMapper, Cabinet> implements ICabinetService {

    @Autowired
    CabinetMapper cabinetMapper;
    @Autowired
    CabinetSlotMapper cabinetSlotMapper;
    @Autowired
    PowerBankService powerBankService;

    /**
     * 柜机分页查询接口
     *
     * @param cabinet
     * @return
     */
    @Override
    public List<Cabinet> selectListCabinet(Cabinet cabinet) {
        return cabinetMapper.selectCabinetList(cabinet);
    }


    /**
     * 搜索未使用柜机
     *
     * @param keyword
     * @return
     */
    @Override
    public List<Cabinet> searchNoUseList(String keyword) {
        LambdaQueryWrapper<Cabinet> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Cabinet::getCabinetNo, keyword);
        lambdaQueryWrapper.eq(Cabinet::getStatus, 0);
        List<Cabinet> cabinetList = cabinetMapper.selectList(lambdaQueryWrapper);
        return cabinetList;
    }


    /**
     * 获取充电宝柜机全部详细信息
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getAllInfo(Long id) {
        // 判断id是否为空

        if (id == 0){
            throw new ServiceException(MessageConstant.PLEASE_SET_CABINET);
        }
        // 查询柜机信息
        Cabinet cabinet = this.getById(id);

        // 查询并获取所有属于该柜机的插槽信息
        List<CabinetSlot> cabinetSlotList = cabinetSlotMapper.selectList(
                new LambdaQueryWrapper<CabinetSlot>().eq(CabinetSlot::getCabinetId, cabinet.getId())
        );

        // 从插槽列表中提取充电宝ID列表（排除null值）
        List<Long> powerBankIdList = cabinetSlotList.stream()
                .filter(item -> null != item.getPowerBankId()) // 过滤掉没有充电宝ID的插槽
                .map(CabinetSlot::getPowerBankId) // 提取充电宝ID
                .collect(Collectors.toList());

        // 如果存在充电宝ID，则查询对应的充电宝信息
        if (!CollectionUtils.isEmpty(powerBankIdList)) {
            // 根据充电宝ID列表查询充电宝信息
            List<PowerBank> powerBankList = powerBankService.listByIds(powerBankIdList);

            // 创建充电宝ID到充电宝对象的映射表
            Map<Long, PowerBank> powerBankIdToPowerBankMap = powerBankList.stream()
                    .collect(Collectors.toMap(PowerBank::getId, powerBank -> powerBank));

            // 遍历插槽列表，为每个插槽设置对应的充电宝对象
            cabinetSlotList.forEach(item -> item.setPowerBank(powerBankIdToPowerBankMap.get(item.getPowerBankId())));
        }

        // 返回包含柜机和插槽信息的结果Map
        return Map.of("cabinet", cabinet, "cabinetSlotList", cabinetSlotList);

    }


    /**
     *根据柜机编号查询
     * @param cabinetNo
     * @return
     */
    @Override
    public Cabinet getBtCabinetNo(String cabinetNo) {
        return cabinetMapper.selectOne(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getCabinetNo, cabinetNo));
    }

}
